home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / sets.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  16KB  |  546 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. """Classes to represent arbitrary sets (including sets of sets).
  5.  
  6. This module implements sets using dictionaries whose values are
  7. ignored.  The usual operations (union, intersection, deletion, etc.)
  8. are provided as both methods and operators.
  9.  
  10. Important: sets are not sequences!  While they support 'x in s',
  11. 'len(s)', and 'for x in s', none of those operations are unique for
  12. sequences; for example, mappings support all three as well.  The
  13. characteristic operation for sequences is subscripting with small
  14. integers: s[i], for i in range(len(s)).  Sets don't support
  15. subscripting at all.  Also, sequences allow multiple occurrences and
  16. their elements have a definite order; sets on the other hand don't
  17. record multiple occurrences and don't remember the order of element
  18. insertion (which is why they don't support s[i]).
  19.  
  20. The following classes are provided:
  21.  
  22. BaseSet -- All the operations common to both mutable and immutable
  23.     sets. This is an abstract class, not meant to be directly
  24.     instantiated.
  25.  
  26. Set -- Mutable sets, subclass of BaseSet; not hashable.
  27.  
  28. ImmutableSet -- Immutable sets, subclass of BaseSet; hashable.
  29.     An iterable argument is mandatory to create an ImmutableSet.
  30.  
  31. _TemporarilyImmutableSet -- A wrapper around a Set, hashable,
  32.     giving the same hash value as the immutable set equivalent
  33.     would have.  Do not use this class directly.
  34.  
  35. Only hashable objects can be added to a Set. In particular, you cannot
  36. really add a Set as an element to another Set; if you try, what is
  37. actually added is an ImmutableSet built from it (it compares equal to
  38. the one you tried adding).
  39.  
  40. When you ask if `x in y' where x is a Set and y is a Set or
  41. ImmutableSet, x is wrapped into a _TemporarilyImmutableSet z, and
  42. what's tested is actually `z in y'.
  43.  
  44. """
  45. from itertools import ifilter, ifilterfalse
  46. __all__ = [
  47.     'BaseSet',
  48.     'Set',
  49.     'ImmutableSet']
  50. import warnings
  51. warnings.warn('the sets module is deprecated', DeprecationWarning, stacklevel = 2)
  52.  
  53. class BaseSet(object):
  54.     '''Common base class for mutable and immutable sets.'''
  55.     __slots__ = [
  56.         '_data']
  57.     
  58.     def __init__(self):
  59.         '''This is an abstract class.'''
  60.         if self.__class__ is BaseSet:
  61.             raise TypeError, 'BaseSet is an abstract class.  Use Set or ImmutableSet.'
  62.  
  63.     
  64.     def __len__(self):
  65.         '''Return the number of elements of a set.'''
  66.         return len(self._data)
  67.  
  68.     
  69.     def __repr__(self):
  70.         """Return string representation of a set.
  71.  
  72.         This looks like 'Set([<list of elements>])'.
  73.         """
  74.         return self._repr()
  75.  
  76.     __str__ = __repr__
  77.     
  78.     def _repr(self, sorted = False):
  79.         elements = self._data.keys()
  80.         if sorted:
  81.             elements.sort()
  82.         return '%s(%r)' % (self.__class__.__name__, elements)
  83.  
  84.     
  85.     def __iter__(self):
  86.         '''Return an iterator over the elements or a set.
  87.  
  88.         This is the keys iterator for the underlying dict.
  89.         '''
  90.         return self._data.iterkeys()
  91.  
  92.     
  93.     def __cmp__(self, other):
  94.         raise TypeError, "can't compare sets using cmp()"
  95.  
  96.     
  97.     def __eq__(self, other):
  98.         if isinstance(other, BaseSet):
  99.             return self._data == other._data
  100.         return None
  101.  
  102.     
  103.     def __ne__(self, other):
  104.         if isinstance(other, BaseSet):
  105.             return self._data != other._data
  106.         return None
  107.  
  108.     
  109.     def copy(self):
  110.         '''Return a shallow copy of a set.'''
  111.         result = self.__class__()
  112.         result._data.update(self._data)
  113.         return result
  114.  
  115.     __copy__ = copy
  116.     
  117.     def __deepcopy__(self, memo):
  118.         '''Return a deep copy of a set; used by copy module.'''
  119.         deepcopy = deepcopy
  120.         import copy
  121.         result = self.__class__()
  122.         memo[id(self)] = result
  123.         data = result._data
  124.         value = True
  125.         for elt in self:
  126.             data[deepcopy(elt, memo)] = value
  127.         
  128.         return result
  129.  
  130.     
  131.     def __or__(self, other):
  132.         '''Return the union of two sets as a new set.
  133.  
  134.         (I.e. all elements that are in either set.)
  135.         '''
  136.         if not isinstance(other, BaseSet):
  137.             return NotImplemented
  138.         return None.union(other)
  139.  
  140.     
  141.     def union(self, other):
  142.         '''Return the union of two sets as a new set.
  143.  
  144.         (I.e. all elements that are in either set.)
  145.         '''
  146.         result = self.__class__(self)
  147.         result._update(other)
  148.         return result
  149.  
  150.     
  151.     def __and__(self, other):
  152.         '''Return the intersection of two sets as a new set.
  153.  
  154.         (I.e. all elements that are in both sets.)
  155.         '''
  156.         if not isinstance(other, BaseSet):
  157.             return NotImplemented
  158.         return None.intersection(other)
  159.  
  160.     
  161.     def intersection(self, other):
  162.         '''Return the intersection of two sets as a new set.
  163.  
  164.         (I.e. all elements that are in both sets.)
  165.         '''
  166.         if not isinstance(other, BaseSet):
  167.             other = Set(other)
  168.         if len(self) <= len(other):
  169.             little = self
  170.             big = other
  171.         else:
  172.             little = other
  173.             big = self
  174.         common = ifilter(big._data.__contains__, little)
  175.         return self.__class__(common)
  176.  
  177.     
  178.     def __xor__(self, other):
  179.         '''Return the symmetric difference of two sets as a new set.
  180.  
  181.         (I.e. all elements that are in exactly one of the sets.)
  182.         '''
  183.         if not isinstance(other, BaseSet):
  184.             return NotImplemented
  185.         return None.symmetric_difference(other)
  186.  
  187.     
  188.     def symmetric_difference(self, other):
  189.         '''Return the symmetric difference of two sets as a new set.
  190.  
  191.         (I.e. all elements that are in exactly one of the sets.)
  192.         '''
  193.         result = self.__class__()
  194.         data = result._data
  195.         value = True
  196.         selfdata = self._data
  197.         
  198.         try:
  199.             otherdata = other._data
  200.         except AttributeError:
  201.             otherdata = Set(other)._data
  202.  
  203.         for elt in ifilterfalse(otherdata.__contains__, selfdata):
  204.             data[elt] = value
  205.         
  206.         for elt in ifilterfalse(selfdata.__contains__, otherdata):
  207.             data[elt] = value
  208.         
  209.         return result
  210.  
  211.     
  212.     def __sub__(self, other):
  213.         '''Return the difference of two sets as a new Set.
  214.  
  215.         (I.e. all elements that are in this set and not in the other.)
  216.         '''
  217.         if not isinstance(other, BaseSet):
  218.             return NotImplemented
  219.         return None.difference(other)
  220.  
  221.     
  222.     def difference(self, other):
  223.         '''Return the difference of two sets as a new Set.
  224.  
  225.         (I.e. all elements that are in this set and not in the other.)
  226.         '''
  227.         result = self.__class__()
  228.         data = result._data
  229.         
  230.         try:
  231.             otherdata = other._data
  232.         except AttributeError:
  233.             otherdata = Set(other)._data
  234.  
  235.         value = True
  236.         for elt in ifilterfalse(otherdata.__contains__, self):
  237.             data[elt] = value
  238.         
  239.         return result
  240.  
  241.     
  242.     def __contains__(self, element):
  243.         """Report whether an element is a member of a set.
  244.  
  245.         (Called in response to the expression `element in self'.)
  246.         """
  247.         
  248.         try:
  249.             return element in self._data
  250.         except TypeError:
  251.             transform = getattr(element, '__as_temporarily_immutable__', None)
  252.             if transform is None:
  253.                 raise 
  254.             return transform() in self._data
  255.  
  256.  
  257.     
  258.     def issubset(self, other):
  259.         '''Report whether another set contains this set.'''
  260.         self._binary_sanity_check(other)
  261.         if len(self) > len(other):
  262.             return False
  263.         for elt in ifilterfalse(other._data.__contains__, self):
  264.             return False
  265.         return True
  266.  
  267.     
  268.     def issuperset(self, other):
  269.         '''Report whether this set contains another set.'''
  270.         self._binary_sanity_check(other)
  271.         if len(self) < len(other):
  272.             return False
  273.         for elt in ifilterfalse(self._data.__contains__, other):
  274.             return False
  275.         return True
  276.  
  277.     __le__ = issubset
  278.     __ge__ = issuperset
  279.     
  280.     def __lt__(self, other):
  281.         self._binary_sanity_check(other)
  282.         if len(self) < len(other):
  283.             pass
  284.         return self.issubset(other)
  285.  
  286.     
  287.     def __gt__(self, other):
  288.         self._binary_sanity_check(other)
  289.         if len(self) > len(other):
  290.             pass
  291.         return self.issuperset(other)
  292.  
  293.     __hash__ = None
  294.     
  295.     def _binary_sanity_check(self, other):
  296.         if not isinstance(other, BaseSet):
  297.             raise TypeError, 'Binary operation only permitted between sets'
  298.  
  299.     
  300.     def _compute_hash(self):
  301.         result = 0
  302.         for elt in self:
  303.             result ^= hash(elt)
  304.         
  305.         return result
  306.  
  307.     
  308.     def _update(self, iterable):
  309.         data = self._data
  310.         if isinstance(iterable, BaseSet):
  311.             data.update(iterable._data)
  312.             return None
  313.         value = None
  314.         if type(iterable) in (list, tuple, xrange):
  315.             it = iter(iterable)
  316.             while True:
  317.                 
  318.                 try:
  319.                     for element in it:
  320.                         data[element] = value
  321.                     
  322.                     return None
  323.                 continue
  324.                 except TypeError:
  325.                     transform = getattr(element, '__as_immutable__', None)
  326.                     if transform is None:
  327.                         raise 
  328.                     data[transform()] = value
  329.                     continue
  330.                 
  331.  
  332.         else:
  333.             for element in iterable:
  334.                 
  335.                 try:
  336.                     data[element] = value
  337.                 continue
  338.                 except TypeError:
  339.                     transform = getattr(element, '__as_immutable__', None)
  340.                     if transform is None:
  341.                         raise 
  342.                     data[transform()] = value
  343.                     continue
  344.                 
  345.  
  346.             
  347.  
  348.  
  349.  
  350. class ImmutableSet(BaseSet):
  351.     '''Immutable set class.'''
  352.     __slots__ = [
  353.         '_hashcode']
  354.     
  355.     def __init__(self, iterable = None):
  356.         '''Construct an immutable set from an optional iterable.'''
  357.         self._hashcode = None
  358.         self._data = { }
  359.         if iterable is not None:
  360.             self._update(iterable)
  361.  
  362.     
  363.     def __hash__(self):
  364.         if self._hashcode is None:
  365.             self._hashcode = self._compute_hash()
  366.         return self._hashcode
  367.  
  368.     
  369.     def __getstate__(self):
  370.         return (self._data, self._hashcode)
  371.  
  372.     
  373.     def __setstate__(self, state):
  374.         (self._data, self._hashcode) = state
  375.  
  376.  
  377.  
  378. class Set(BaseSet):
  379.     ''' Mutable set class.'''
  380.     __slots__ = []
  381.     
  382.     def __init__(self, iterable = None):
  383.         '''Construct a set from an optional iterable.'''
  384.         self._data = { }
  385.         if iterable is not None:
  386.             self._update(iterable)
  387.  
  388.     
  389.     def __getstate__(self):
  390.         return (self._data,)
  391.  
  392.     
  393.     def __setstate__(self, data):
  394.         (self._data,) = data
  395.  
  396.     
  397.     def __ior__(self, other):
  398.         '''Update a set with the union of itself and another.'''
  399.         self._binary_sanity_check(other)
  400.         self._data.update(other._data)
  401.         return self
  402.  
  403.     
  404.     def union_update(self, other):
  405.         '''Update a set with the union of itself and another.'''
  406.         self._update(other)
  407.  
  408.     
  409.     def __iand__(self, other):
  410.         '''Update a set with the intersection of itself and another.'''
  411.         self._binary_sanity_check(other)
  412.         self._data = (self & other)._data
  413.         return self
  414.  
  415.     
  416.     def intersection_update(self, other):
  417.         '''Update a set with the intersection of itself and another.'''
  418.         if isinstance(other, BaseSet):
  419.             self &= other
  420.         else:
  421.             self._data = self.intersection(other)._data
  422.  
  423.     
  424.     def __ixor__(self, other):
  425.         '''Update a set with the symmetric difference of itself and another.'''
  426.         self._binary_sanity_check(other)
  427.         self.symmetric_difference_update(other)
  428.         return self
  429.  
  430.     
  431.     def symmetric_difference_update(self, other):
  432.         '''Update a set with the symmetric difference of itself and another.'''
  433.         data = self._data
  434.         value = True
  435.         if not isinstance(other, BaseSet):
  436.             other = Set(other)
  437.         if self is other:
  438.             self.clear()
  439.         for elt in other:
  440.             if elt in data:
  441.                 del data[elt]
  442.                 continue
  443.             data[elt] = value
  444.         
  445.  
  446.     
  447.     def __isub__(self, other):
  448.         '''Remove all elements of another set from this set.'''
  449.         self._binary_sanity_check(other)
  450.         self.difference_update(other)
  451.         return self
  452.  
  453.     
  454.     def difference_update(self, other):
  455.         '''Remove all elements of another set from this set.'''
  456.         data = self._data
  457.         if not isinstance(other, BaseSet):
  458.             other = Set(other)
  459.         if self is other:
  460.             self.clear()
  461.         for elt in ifilter(data.__contains__, other):
  462.             del data[elt]
  463.         
  464.  
  465.     
  466.     def update(self, iterable):
  467.         '''Add all values from an iterable (such as a list or file).'''
  468.         self._update(iterable)
  469.  
  470.     
  471.     def clear(self):
  472.         '''Remove all elements from this set.'''
  473.         self._data.clear()
  474.  
  475.     
  476.     def add(self, element):
  477.         '''Add an element to a set.
  478.  
  479.         This has no effect if the element is already present.
  480.         '''
  481.         
  482.         try:
  483.             self._data[element] = True
  484.         except TypeError:
  485.             transform = getattr(element, '__as_immutable__', None)
  486.             if transform is None:
  487.                 raise 
  488.             self._data[transform()] = True
  489.  
  490.  
  491.     
  492.     def remove(self, element):
  493.         '''Remove an element from a set; it must be a member.
  494.  
  495.         If the element is not a member, raise a KeyError.
  496.         '''
  497.         
  498.         try:
  499.             del self._data[element]
  500.         except TypeError:
  501.             transform = getattr(element, '__as_temporarily_immutable__', None)
  502.             if transform is None:
  503.                 raise 
  504.             del self._data[transform()]
  505.  
  506.  
  507.     
  508.     def discard(self, element):
  509.         '''Remove an element from a set if it is a member.
  510.  
  511.         If the element is not a member, do nothing.
  512.         '''
  513.         
  514.         try:
  515.             self.remove(element)
  516.         except KeyError:
  517.             pass
  518.  
  519.  
  520.     
  521.     def pop(self):
  522.         '''Remove and return an arbitrary set element.'''
  523.         return self._data.popitem()[0]
  524.  
  525.     
  526.     def __as_immutable__(self):
  527.         return ImmutableSet(self)
  528.  
  529.     
  530.     def __as_temporarily_immutable__(self):
  531.         return _TemporarilyImmutableSet(self)
  532.  
  533.  
  534.  
  535. class _TemporarilyImmutableSet(BaseSet):
  536.     
  537.     def __init__(self, set):
  538.         self._set = set
  539.         self._data = set._data
  540.  
  541.     
  542.     def __hash__(self):
  543.         return self._set._compute_hash()
  544.  
  545.  
  546.